#include <bits/stdc++.h>
using namespace std;

int n, m, EnteringDegree[1001], input[1001];
vector<int> g[1001];
bool connect[1001][1001], vis[1001];


int topologicalSort()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        if (EnteringDegree[i] == 0)
        {
            q.push(i);
        }
    }
    int cnt = 0; 
    while (!q.empty())
    {
        int s = q.size();
        for (int i = 0; i < s; i++)
        {
            int u = q.front();
            q.pop();
            for (auto v : g[u])
            {
                EnteringDegree[v]--;
                if (EnteringDegree[v] == 0)
                    q.push(v);
            }
        }
        cnt++; 
    }
    return cnt;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int num;
        cin >> num;
        memset(vis, 0, sizeof(vis));
        for (int j = 0; j < num; j++)
        {
            cin >> input[j];
            vis[input[j]] = true; 
        }
        for (int j = 0; j < num; j++)
        {
            for (int k = input[0]; k <= input[num - 1]; k++)
            {
                if (!vis[k] && !connect[input[j]][k])
                {
                    connect[input[j]][k] = true; 
                    g[k].push_back(input[j]);
                    EnteringDegree[input[j]]++;
                }
            }
        }
    }

    cout << topologicalSort() << endl;

    return 0;
}